﻿<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8"/>
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <title>数据源管理</title>
    <link rel="stylesheet" href="/static/assets/libs/layui/css/layui.css"/>
    <link rel="stylesheet" href="/static/assets/module/admin.css?v=318"/>
    <link rel="stylesheet" href="/static/assets/css/table.css"/>
    <!--[if lt IE 9]>
    <script src="https://oss.maxcdn.com/html5shiv/3.7.3/html5shiv.min.js"></script>
    <script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>
    <![endif]-->
</head>
<body>
<!-- 正文开始 -->
<div class="layui-fluid">
    <div class="layui-card">
        <div class="layui-card-body">
            <!-- 表格工具栏 -->
            <form class="layui-form toolbar">
                <div class="layui-inline">
                    <label class="layui-form-label">关键词:</label>
                    <div class="layui-input-inline">
                        <input name="keyword" class="layui-input" placeholder="输入名称"/>
                    </div>
                </div>
                <div class="layui-inline">&emsp;
                    <button class="layui-btn icon-btn" lay-filter="dscTbSearch" lay-submit>
                        <i class="layui-icon">&#xe615;</i>搜索
                    </button>
                </div>
            </form>
        </div>
    </div>
    <div class="layui-card">
        <div class="layui-card-body">
            <!-- 数据表格 -->
            <table id="dscTable" lay-filter="dscTable"></table>
        </div>
    </div>
</div>

<!-- 表格操作列 -->
<script type="text/html" id="dscTbBar">
    <a class="layui-btn layui-btn-primary layui-btn-xs" lay-event="edit">编辑</a>
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
    <!--<a class="layui-btn layui-btn-xs" lay-event="testUrl">测试</a>-->
</script>
<!-- 表单弹窗 -->
<script type="text/html" id="dscEditDialog">
    <form id="dscEditForm" lay-filter="dscEditForm" class="layui-form model-form">
        <input name="id" type="hidden"/>
        <div class="layui-form-item">
            <label class="layui-form-label layui-form-required">链接地址:</label>
            <div class="layui-input-block">
                <input name="url" placeholder="地址" class="layui-input"
                       lay-verType="tips" lay-verify="required" required/>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label layui-form-required">用户名:</label>
            <div class="layui-input-block">
                <input name="username" placeholder="请输入用户名" class="layui-input"
                       lay-verType="tips" lay-verify="required" required/>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">密码:</label>
            <div class="layui-input-block">
                <input name="password" placeholder="请输入密码" class="layui-input"/>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label layui-form-required">驱动:</label>
            <div class="layui-input-block">
                <input name="driverName" placeholder="请输入用户名" class="layui-input"
                       lay-verType="tips" lay-verify="required" required/>
            </div>
        </div>
        <div class="layui-form-item text-right">
            <button class="layui-btn layui-btn-warm" lay-filter="dscTestSubmit" lay-submit>测试连接</button>
            <button class="layui-btn" lay-filter="dscEditSubmit" lay-submit>保存</button>
            <button class="layui-btn layui-btn-primary" type="button" ew-event="closeDialog">取消</button>
        </div>
    </form>
</script>

<script type="text/html" id="toolbarOepration">
    <button class="layui-btn layui-btn-sm" lay-event="add" ><i
            class="layui-icon">&#xe654;</i>新建数据源</button>
    <button class="layui-btn layui-btn-sm layui-btn-danger" lay-event="batchDel">
        <i class="layui-icon layui-icon-delete"></i>批量删除</button>
</script>

<!-- js部分 -->
<script type="text/javascript" src="/static/assets/libs/jquery/jquery-3.2.1.min.js"></script>
<script type="text/javascript" src="/static/assets/libs/layui/layui.js"></script>
<script type="text/javascript" src="/static/assets/js/common.js?v=318"></script>
<script type="text/javascript" src="/static/assets/js/ajaxUtil.js?v=318"></script>
<script>
    layui.use(['layer', 'form', 'table', 'util', 'admin', 'formX'], function () {
        var $ = layui.jquery;
        var layer = layui.layer;
        var form = layui.form;
        var table = layui.table;
        var util = layui.util;
        var admin = layui.admin;
        var formX = layui.formX;

        /* 渲染表格 */
        var insTb = table.render({
            elem: '#dscTable',
            url: '/datasource/',
            page: true,
            skin: 'line',
            toolbar: '#toolbarOepration',
            cellMinWidth: 100,
            cols: [
                [
                {type: 'checkbox'},
                {field: 'id', title: 'ID',width:30},
                {field: 'url', title: '数据源地址',width:600},
                {field: 'username', title: '用户名',width:150},
                /*{field: 'password', title: '密码'},*/
                {field: 'driverName', title: '驱动',width:200},
                {title: '操作', toolbar: '#dscTbBar', align: 'center', minWidth: 200}
            ]
            ]
        });

        /* 表格搜索 */
        form.on('submit(dscTbSearch)', function (data) {
            insTb.reload({where: data.field, page: {curr: 1}});
            return false;
        });

        /* 表格工具条点击事件 */
        table.on('tool(dscTable)', function (obj) {
            if (obj.event === 'edit') { // 修改
                showEditModel(obj.data);
            } else if (obj.event === 'del') { // 删除
                doDel(obj);
            } else if (obj.event === 'testUrl') { // test
                doTest(obj.data.id);
            }
        });

        /* 表格头工具栏点击事件 */
        table.on('toolbar(dscTable)', function (obj) {
            if (obj.event === 'add') { // 添加
                showEditModel();
            } else if (obj.event === 'batchDel') { // 删除
                var checkRows = table.checkStatus('dscTable');
                if (checkRows.data.length === 0) {
                    layer.msg('请选择要删除的数据', {icon: 2});
                    return;
                }
                var ids = checkRows.data.map(function (d) {
                    return d.id;
                });
                doDel({ids: ids});
            }
        });

        /* 删除 */
        function doDel(obj) {
            layer.confirm('确定要删除选中数据吗？', {
                skin: 'layui-layer-admin',
                shade: .1
            }, function (i) {
                layer.close(i);
                var loadIndex = layer.load(2);
                B.del({
                    url:'/table/'+obj.data.tableId,
                    success:function (res) {
                        layer.close(loadIndex);
                        if (0 === res.code) {
                            layer.msg(res.msg, {icon: 1});
                            insTb.reload({page: {curr: 1}});
                        } else {
                            layer.msg(res.msg, {icon: 2});
                        }
                    }
                });
            });
        }
        /* 显示表单弹窗 */
        function showEditModel(mData) {
            admin.open({
                type: 1,
                title: (mData ? '修改' : '添加') + '数据源',
                content: $('#dscEditDialog').html(),
                success: function (layero, dIndex) {
                    // 回显表单数据
                    form.val('dscEditForm', mData);
                    // 表单提交事件
                    form.on('submit(dscEditSubmit)', function (data) {
                        var loadIndex = layer.load(2);
                        $.ajax({
                            url: '/datasource/',
                            data: JSON.stringify(data.field),
                            dataType: 'json',
                            method: mData ? 'PUT' : 'POST',
                            contentType: 'application/json;charset=utf-8',
                            success: function (res) {
                                layer.close(loadIndex);
                                if (0 === res.code) {
                                    layer.close(dIndex);
                                    layer.msg(res.msg, {icon: 1});
                                    insTb.reload();
                                } else {
                                    layer.msg(res.msg, {icon: 2});
                                }
                            }
                        });
                        return false;
                    });
                    // 测试
                    form.on('submit(dscTestSubmit)', function (data) {
                        $.ajax({
                            url: '/datasource/test',
                            data: JSON.stringify(data.field),
                            dataType: 'json',
                            method: 'POST',
                            contentType: 'application/json;charset=utf-8',
                            success: function (res) {
                                if (0 === res.code) {
                                    top.layer.msg(res.msg, {icon: 1});
                                } else {
                                    top.layer.msg(res.msg, {icon: 2});
                                }
                            }
                        });
                        return false;
                    });
                    // 禁止弹窗出现滚动条
                    $(layero).children('.layui-layer-content').css('overflow', 'visible');
                }
            });
        }

        /* 删除 */
        function doDel(obj) {
            layer.confirm('确定要删除选中数据吗？', {
                skin: 'layui-layer-admin',
                shade: .1
            }, function (i) {
                layer.close(i);
                var loadIndex = layer.load(2);
                B.del({
                    url:'/datasource/'+obj.data.id,
                    success:function (res) {
                        layer.close(loadIndex);
                        if (0 === res.code) {
                            layer.msg(res.msg, {icon: 1});
                            insTb.reload({page: {curr: 1}});
                        } else {
                            layer.msg(res.msg, {icon: 2});
                        }
                    }
                });
            });
        }
        function doTest(id) {
            var loadIndex = layer.load(2);
            $.ajax({
                url: '/datasource/test/'+id,
                dataType: 'json',
                method: 'POST',
                contentType: 'application/json;charset=utf-8',
                success: function (res) {
                    layer.close(loadIndex);
                    if (0 === res.code) {
                        layer.close(dIndex);
                        layer.msg(res.msg, {icon: 1});
                    } else {
                        layer.msg(res.msg, {icon: 2});
                    }
                }
            });
        }

    });
</script>
</body>
</html>
